package com.example.myday1909d.ui;


import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.example.myday1909d.Base.BaseRlvAdapter;
import com.example.myday1909d.Base.Constants;
import com.example.myday1909d.R;
import com.example.myday1909d.p.ItInfoItemPresenter;
import com.example.myday1909d.v.ItInfoItemView;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class ItInfoItemFragment extends BaseLazyFragment<ItInfoItemPresenter> implements ItInfoItemView{

    private String mTitle;
    private int mId;

    private int mPage = 1;
    private BaseRlvAdapter<ItInfoItemBean.DataBean.DatasBean> mAdapter;

    public static ItInfoItemFragment getInstance(String tabTitle, int id){
        ItInfoItemFragment fragment = new ItInfoItemFragment();
        Bundle bundle = new Bundle();
        bundle.putString(Constants.TITLE,tabTitle);
        bundle.putInt(Constants.ID,id);
        fragment.setArguments(bundle);

        return fragment;
    }

    @BindView(R.id.rlv)
    RecyclerView mRlv;

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        mPresenter.getData(mPage,mId);
    }

    @Override
    protected ItInfoItemPresenter initPresenter() {
        return new ItInfoItemPresenter();
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void initView(View view) {
        Bundle arguments = getArguments();
        mTitle = arguments.getString(Constants.TITLE);
        mId = arguments.getInt(Constants.ID);


        ArrayList<ItInfoItemBean.DataBean.DatasBean> list = new ArrayList<>();
        mAdapter = new BaseRlvAdapter<ItInfoItemBean.DataBean.DatasBean>(getContext(), list) {
            @Override
            protected void bindData(BaseViewHolder holder, ItInfoItemBean.DataBean.DatasBean datasBean) {
                holder.setText(R.id.tv_author, datasBean.getAuthor());
                holder.setText(R.id.tv_time, datasBean.getNiceDate());
                holder.setText(R.id.tv_title, datasBean.getTitle());
                holder.setText(R.id.tv_wechat, datasBean.getChapterName());
            }

            @Override
            protected int getLayout() {
                return R.layout.item_it_info;
            }
        };
        mRlv.setLayoutManager(new LinearLayoutManager(getContext()));
        mRlv.setAdapter(mAdapter);
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_itinfo_item;
    }

    @Override
    public void setData(ItInfoItemBean itInfoItemBean) {
        mAdapter.addData(itInfoItemBean.getData().getDatas());
    }
}
